using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

using UMEVOR.DTO;

using Microsoft.Practices.EnterpriseLibrary.Data;

namespace UMEVOR.DAL
{
    public class DALT_CATEGORIA : DALConnection
    {

        #region Incluir
        public void incluir(T_CATEGORIA obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" INSERT INTO T_CATEGORIA ");
                SQL.Append("    (TX_DESC_CATEGORIA) ");
                SQL.Append(" VALUES ");
                SQL.Append("    ('" + obj.TX_DESC_CATEGORIA + "') ");

                DbCommand insertT_CATEGORIA = database.GetSqlStringCommand(SQL.ToString());

                insertT_CATEGORIA.CommandText = SQL.ToString();

                database.ExecuteNonQuery(insertT_CATEGORIA, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Incluir[]

        public void incluir(T_CATEGORIA[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] insertT_CATEGORIA = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_CATEGORIA o = obj[index];
                    SQL = new StringBuilder();

                    SQL.Append(" INSERT INTO T_CATEGORIA ");
                    SQL.Append("    (ID_CATEGORIA, TX_DESC_CATEGORIA) ");
                    SQL.Append(" VALUES ");
                    SQL.Append("    (:ID_CATEGORIA, :TX_DESC_CATEGORIA) ");

                    insertT_CATEGORIA[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(insertT_CATEGORIA[index], ":ID_CATEGORIA", DbType.Int64, o.ID_CATEGORIA);
                    database.AddInParameter(insertT_CATEGORIA[index], ":TX_DESC_CATEGORIA", DbType.String, o.TX_DESC_CATEGORIA);

                    insertT_CATEGORIA[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in insertT_CATEGORIA)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Alterar

        public void alterar(T_CATEGORIA obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" UPDATE T_CATEGORIA SET ");
                SQL.Append("    TX_DESC_CATEGORIA = '" + obj.TX_DESC_CATEGORIA + "' ");
                SQL.Append(" WHERE ");
                SQL.Append("     ID_CATEGORIA = " + obj.ID_CATEGORIA.ToString());

                DbCommand updateT_CATEGORIA = database.GetSqlStringCommand(SQL.ToString());

                updateT_CATEGORIA.CommandText = SQL.ToString();

                database.ExecuteNonQuery(updateT_CATEGORIA, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Alterar[]

        public void alterar(T_CATEGORIA[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] updateT_CATEGORIA = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_CATEGORIA o = obj[index];
                    SQL = new StringBuilder();
                    SQL.Append(" UPDATE T_CATEGORIA SET ");
                    SQL.Append("    TX_DESC_CATEGORIA = :TX_DESC_CATEGORIA ");
                    SQL.Append(" WHERE ");
                    SQL.Append("     ID_CATEGORIA = :ID_CATEGORIA  ");

                    updateT_CATEGORIA[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(updateT_CATEGORIA[index], ":ID_CATEGORIA", DbType.Int64, o.ID_CATEGORIA);
                    database.AddInParameter(updateT_CATEGORIA[index], ":TX_DESC_CATEGORIA", DbType.String, o.TX_DESC_CATEGORIA);

                    updateT_CATEGORIA[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in updateT_CATEGORIA)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Excluir

        public void excluir(T_CATEGORIA obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" DELETE FROM T_CATEGORIA ");
                SQL.Append(" WHERE ");
                SQL.Append("     ID_CATEGORIA = " + obj.ID_CATEGORIA.ToString());

                DbCommand deleteT_CATEGORIA = database.GetSqlStringCommand(SQL.ToString());

                deleteT_CATEGORIA.CommandText = SQL.ToString();

                database.ExecuteNonQuery(deleteT_CATEGORIA, DALConnection.transaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Excluir[]

        public void excluir(T_CATEGORIA[] obj)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();
                DbCommand[] deleteT_CATEGORIA = new DbCommand[obj.Length];

                for (int index = 0; index < obj.Length; index++)
                {
                    T_CATEGORIA o = obj[index];
                    SQL = new StringBuilder();
                    SQL.Append(" DELETE FROM T_CATEGORIA ");
                    SQL.Append(" WHERE ");
                    SQL.Append("     ID_CATEGORIA = :ID_CATEGORIA  ");

                    deleteT_CATEGORIA[index] = database.GetSqlStringCommand(SQL.ToString());

                    database.AddInParameter(deleteT_CATEGORIA[index], ":ID_CATEGORIA", DbType.Int64, o.ID_CATEGORIA);

                    deleteT_CATEGORIA[index].CommandText = SQL.ToString();
                }

                try
                {
                    foreach (DbCommand cmd in deleteT_CATEGORIA)
                    {
                        database.ExecuteNonQuery(cmd, DALConnection.transaction);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region ObterPorPK

        public T_CATEGORIA obterPorPK(long pID_CATEGORIA)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" SELECT * FROM T_CATEGORIA ");
                SQL.Append(" WHERE ");
                SQL.Append("  ID_CATEGORIA = " + pID_CATEGORIA.ToString());

                DbCommand consultaT_CATEGORIA = database.GetSqlStringCommand(SQL.ToString());
                consultaT_CATEGORIA.CommandText = SQL.ToString();

                IDataReader reader = database.ExecuteReader(consultaT_CATEGORIA, DALConnection.transaction);
                T_CATEGORIA obj = null;

                if (reader.Read())
                {
                    obj = new T_CATEGORIA();
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_CATEGORIA")))
                    {
                        obj.ID_CATEGORIA = Int64.Parse(reader["ID_CATEGORIA"].ToString());
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("TX_DESC_CATEGORIA")))
                    {
                        obj.TX_DESC_CATEGORIA = reader["TX_DESC_CATEGORIA"].ToString();
                    }
                }
                reader.Dispose();
                consultaT_CATEGORIA.Dispose();

                return obj;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region ListarTodos

        public List<T_CATEGORIA> listarTodos()
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" SELECT * FROM T_CATEGORIA ORDER BY TX_DESC_CATEGORIA");

                DbCommand consultaT_CATEGORIA = database.GetSqlStringCommand(SQL.ToString());
                consultaT_CATEGORIA.CommandText = SQL.ToString();

                IDataReader reader = database.ExecuteReader(consultaT_CATEGORIA, DALConnection.transaction);
                List<T_CATEGORIA> lista = new List<T_CATEGORIA>();
                T_CATEGORIA obj = null;

                while (reader.Read())
                {
                    obj = new T_CATEGORIA();
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_CATEGORIA")))
                    {
                        obj.ID_CATEGORIA = Int64.Parse(reader["ID_CATEGORIA"].ToString());
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("TX_DESC_CATEGORIA")))
                    {
                        obj.TX_DESC_CATEGORIA = reader["TX_DESC_CATEGORIA"].ToString();
                    }
                    lista.Add(obj);
                }
                reader.Dispose();
                consultaT_CATEGORIA.Dispose();

                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<T_CATEGORIA> consultar(string descricao)
        {
            StringBuilder SQL = new StringBuilder();
            try
            {
                Database database = getDatabase();

                SQL.Append(" SELECT * FROM T_CATEGORIA");
                SQL.Append(" WHERE TX_DESC_CATEGORIA LIKE '" + descricao + "%' ORDER BY TX_DESC_CATEGORIA");

                DbCommand consultaT_CATEGORIA = database.GetSqlStringCommand(SQL.ToString());
                consultaT_CATEGORIA.CommandText = SQL.ToString();

                IDataReader reader = database.ExecuteReader(consultaT_CATEGORIA, DALConnection.transaction);
                List<T_CATEGORIA> lista = new List<T_CATEGORIA>();
                T_CATEGORIA obj = null;

                while (reader.Read())
                {
                    obj = new T_CATEGORIA();
                    if (!reader.IsDBNull(reader.GetOrdinal("ID_CATEGORIA")))
                    {
                        obj.ID_CATEGORIA = Int64.Parse(reader["ID_CATEGORIA"].ToString());
                    }
                    if (!reader.IsDBNull(reader.GetOrdinal("TX_DESC_CATEGORIA")))
                    {
                        obj.TX_DESC_CATEGORIA = reader["TX_DESC_CATEGORIA"].ToString();
                    }
                    lista.Add(obj);

                }
                reader.Dispose();
                consultaT_CATEGORIA.Dispose();

                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}

